package io.renren.modules.sys.controller;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.renren.common.dto.SongSetSong;
import io.renren.modules.sys.entity.SongEntity;
import io.renren.modules.sys.entity.SongsetSongEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SongsetEntity;
import io.renren.modules.sys.service.SongsetService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author wangyi
 * @email 794651397@qq.com
 * @date 2018-02-24 10:46:32
 */
@RestController
@RequestMapping("/sys/songset")
public class SongsetController {
	@Autowired
	private SongsetService songsetService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SongsetEntity> songsetList = songsetService.queryList(query);
		int total = songsetService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(songsetList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		SongsetEntity songset = songsetService.queryObject(id);

		return R.ok().put("songset", songset);
	}

    /**
     * 信息
     */
    @RequestMapping("/getSong/{id}")
    public R getSong(@PathVariable("id") Long id){
        List<SongEntity> list=songsetService.getSongBySetId(id);
        return R.ok().put("list", list);
    }
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody SongsetEntity songset){
        songset.setCreateTime(new Date());
        songset.setReleaseTime(new Date());
		songsetService.save(songset);
		
		return R.ok().put("songsetId",songset.getId());
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody SongsetEntity songset){
	    songset.setUpdateTime(new Date());
		songsetService.update(songset);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids){
		songsetService.deleteBatch(ids);
		
		return R.ok();
	}

    /**
     * 关联曲谱
     */
    @RequestMapping("/songSetSong")
    public R songSetSong(@RequestBody SongSetSong songSetSong){
        Integer SongsetId=songSetSong.getSongSetId();
        Integer[] songIds=songSetSong.getSongIds();
        songsetService.deleteSongSetSong(SongsetId);
        List<SongsetSongEntity> l=new LinkedList<>();
        for (Integer songId : songIds) {
            SongsetSongEntity s=new SongsetSongEntity();
            s.setSongId(songId.longValue());
            s.setSongsetId(SongsetId.longValue());
            s.setCreateTime(new Date());
            l.add(s);
        }
        songsetService.addSongSetSongBatch(l);
        return R.ok();
    }
	
}
